home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer 2.0 / Internet Surfer 2.0 (Wayzata Technology) (1996).iso / pc / text / mac / faqs.538 < prev    next >
Encoding:
Text File  |  1996-02-12  |  27.8 KB  |  658 lines

  1. Frequently Asked Questions (FAQS);faqs.538
  2.  
  3.  
  4.  
  5.       Some vendors provide a "/proc" filesystem, which appears as a
  6.       directory with a bunch of filenames in it.  Each filename is a
  7.       number, corresponding to a process ID, and you can open the file
  8.       and read it to get information about the process.  Once again,
  9.       access to this may be restricted, and the interface to it may
  10.       change from system to system.
  11.  
  12.       If you can't use vendor-specific library functions, and you
  13.       don't have /proc, and you still want to do this completely
  14.       in C, you
  15.       are going to have to do the grovelling through kernel memory
  16.       yourself.  For a good example of how to do this on many systems,
  17.       see the sources to "ofiles", available in the comp.sources.unix
  18.       archives.  (A package named "kstuff" to help with kernel
  19.       grovelling was posted to alt.sources in May 1991 and is also
  20.       available via anonymous ftp as
  21.       usenet/alt.sources/articles/{329{6,7,8,9},330{0,1}}.Z from
  22.       wuarchive.wustl.edu.)
  23.  
  24. 3.11) How do I check the exit status of a remote command
  25.       executed via "rsh" ?
  26.  
  27.       This doesn't work:
  28.  
  29.     rsh some-machine some-crummy-command || echo "Command failed"
  30.  
  31.       The exit status of 'rsh' is 0 (success) if the rsh program
  32.       itself completed successfully, which probably isn't what
  33.       you wanted.
  34.  
  35.       If you want to check on the exit status of the remote program,
  36.       you can try using Maarten Litmaath's 'ersh' script, which was
  37.       posted to alt.sources in January, 1991.  ersh is a shell script
  38.       that calls rsh, arranges for the remote machine to echo the
  39.       status of the command after it completes, and exits with that
  40.       status.
  41.  
  42. 3.12) Is it possible to pass shell variable settings into an awk program?
  43.  
  44.       There are two different ways to do this.  The first involves
  45.       simply expanding the variable where it is needed in the program.
  46.       For example, to get a list of all ttys you're using:
  47.  
  48.     who | awk '/^'"$USER"'/ { print $2 }'                (1)
  49.  
  50.       Single quotes are usually used to enclose awk programs because
  51.       the character '$' is often used in them, and '$' will be
  52.       interpreted by the shell if enclosed inside double quotes, but
  53.       not if enclosed inside single quotes.  In this case, we *want*
  54.       the '$' in "$USER" to be interpreted by the shell, so we close
  55.       the single quotes and then put the "$USER" inside double quotes.
  56.       Note that there are no spaces in any of that, so the shell will
  57.       see it all as one argument.  Note, further, that the double
  58.       quotes probably aren't necessary in this particular case (i.e. we
  59.       could have done
  60.  
  61.     who | awk '/^'$USER'/ { print $2 }'                (2)
  62.  
  63.       ), but they should be included nevertheless because they are
  64.       necessary when the shell variable in question contains special
  65.       characters or spaces.
  66.  
  67.       The second way to pass variable settings into awk is to use an
  68.       often undocumented feature of awk which allows variable settings
  69.       to be specified as "fake file names" on the command line.  For
  70.       example:
  71.  
  72.     who | awk '$1 == user { print $2 }' user="$USER" -        (3)
  73.  
  74.       Variable settings take effect when they are encountered on the
  75.       command line, so, for example, you could instruct awk on how to
  76.       behave for different files using this technique.  For example:
  77.  
  78.     awk '{ program that depends on s }' s=1 file1 s=0 file2        (4)
  79.  
  80.       Note that some versions of awk will cause variable settings
  81.       encountered before any real filenames to take effect before the
  82.       BEGIN block is executed, but some won't so neither way should be
  83.       relied upon.
  84.  
  85.       Note, further, that when you specify a variable setting, awk
  86.       won't automatically read from stdin if no real files are
  87.       specified, so you need to add a "-" argument to the end of your
  88.       command, as I did at (3) above.
  89.  
  90. 3.13) How do I get rid of zombie processes that persevere?
  91.  
  92.       From: jik@pit-manager.MIT.Edu (Jonathan I. Kamens)
  93.       Date: Fri, 17 Jan 92 14:40:09 -0500
  94.  
  95.       Unfortunately, it's impossible to generalize how the death of
  96.       child processes should behave, because the exact mechanism varies
  97.       over the various flavors of Unix.
  98.  
  99.       First of all, by default, you have to do a wait() for child
  100.       processes under ALL flavors of Unix.  That is, there is no flavor
  101.       of Unix that I know of that will automatically flush child
  102.       processes that exit, even if you don't do anything to tell it to
  103.       do so.
  104.  
  105.       Second, under some SysV-derived systems, if you do
  106.       "signal(SIGCHLD, SIG_IGN)" (well, actually, it may be SIGCLD
  107.       instead of SIGCHLD, but most of the newer SysV systems have
  108.       "#define SIGCHLD SIGCLD" in the header files), then child
  109.       processes will be cleaned up automatically, with no further
  110.       effort in your part.  The best way to find out if it works at
  111.       your site is to try it, although if you are trying to write
  112.       portable code, it's a bad idea to rely on this in any case.
  113.       Unfortunately, POSIX doesn't allow you to do this; the behavior
  114.       of setting the SIGCHLD to SIG_IGN under POSIX is undefined, so
  115.       you can't do it if your program is supposed to be
  116.       POSIX-compliant.
  117.  
  118.       If you can't use SIG_IGN to force automatic clean-up, then you've
  119.       got to write a signal handler to do it.  It isn't easy at all to
  120.       write a signal handler that does things right on all flavors of
  121.       Unix, because of the following inconsistencies:
  122.  
  123.       On some flavors of Unix, the SIGCHLD signal handler is called if
  124.       one *or more* children have died.  This means that if your signal
  125.       handler only does one wait() call, then it won't clean up all of
  126.       the children.  Fortunately, I believe that all Unix flavors for
  127.       which this is the case have available to the programmer the
  128.       wait3() call, which allows the WNOHANG option to check whether or
  129.       not there are any children waiting to be cleaned up.  Therefore,
  130.       on any system that has wait3(), your signal handler should call
  131.       wait3() over and over again with the WNOHANG option until there
  132.       are no children left to clean up.
  133.  
  134.       On SysV-derived systems, SIGCHLD signals are regenerated if there
  135.       are child processes still waiting to be cleaned up after you exit
  136.       the SIGCHLD signal handler.  Therefore, it's safe on most SysV
  137.       systems to assume when the signal handler gets called that you
  138.       only have to clean up one signal, and assume that the handler
  139.       will get called again if there are more to clean up after it
  140.       exits.
  141.  
  142.       On older systems, signal handlers are automatically reset to
  143.       SIG_DFL when the signal handler gets called.  On such systems,
  144.       you have to put "signal(SIGCHILD, catcher_func)" (where
  145.       "catcher_func" is the name of the handler function) as the first
  146.       thing in the signal handler, so that it gets reset.
  147.       Unfortunately, there is a race condition which may cause you to
  148.       get a SIGCHLD signal and have it ignored between the time your
  149.       handler gets called and the time you reset the signal.
  150.       Fortunately, newer implementations of signal() don't reset the
  151.       handler to SIG_DFL when the handler function is called.  To get
  152.       around this problem, on systems that do not have wait3() but do
  153.       have SIGCLD, you need to reset the signal handler with a call to
  154.       signal() after doing at least one wait() within the handler, each
  155.       time it is called.
  156.  
  157.       The summary of all this is that on systems that have wait3(), you
  158.       should use that and your signal handler should loop, and on
  159.       systems that don't, you should have one call to wait() per
  160.       invocation of the signal handler.
  161.  
  162.       One more thing -- if you don't want to go through all of this
  163.       trouble, there is a portable way to avoid this problem, although
  164.       it is somewhat less efficient.  Your parent process should fork,
  165.       and then wait right there and then for the child process to
  166.       terminate.  The child process then forks again, giving you a
  167.       child and a grandchild.  The child exits immediately (and hence
  168.       the parent waiting for it notices its death and continues to
  169.       work), and the grandchild does whatever the child was originally
  170.       supposed to.  Since its parent died, it is inherited by init,
  171.       which will do whatever waiting is needed.  This method is
  172.       inefficient because it requires an extra fork, but is pretty much
  173.       completely portable.
  174.  
  175. 3.14) How do I get lines from a pipe as they are written instead of only in
  176.       larger blocks.
  177.  
  178.       From: jik@pit-manager.MIT.Edu (Jonathan I. Kamens)
  179.       Date: Sun, 16 Feb 92 20:59:28 -0500
  180.  
  181.       The stdio library does buffering differently depending on whether
  182.       it thinks it's running on a tty.  If it thinks it's on a tty, it
  183.       does buffering on a per-line basis; if not, it uses a larger
  184.       buffer than one line.
  185.  
  186.       If you have the source code to the client whose buffering you
  187.       want to disable, you can use setbuf() or setvbuf() to change the
  188.       buffering.
  189.  
  190.       If not, the best you can do is try to convince the program that
  191.       it's running on a tty by running it under a pty, e.g. by using
  192.       the "pty" program mentioned in question 3.9.
  193.  
  194. --
  195. Ted Timar - tmatimar@empress.com
  196. Empress Software, 3100 Steeles Ave E, Markham, Ont., Canada L3R 8T3
  197. Xref: bloom-picayune.mit.edu comp.unix.questions:51336 comp.unix.shell:8342 news.answers:4778
  198. Path: bloom-picayune.mit.edu!senator-bedfellow.mit.edu!senator-bedfellow.mit.edu!usenet
  199. From: tmatimar@empress.com (Ted M A Timar)
  200. Newsgroups: comp.unix.questions,comp.unix.shell,news.answers
  201. Subject: Unix - Frequently Asked Questions (4/7) [Frequent posting]
  202. Supersedes: <unix-faq/faq/part4_723967331@athena.mit.edu>
  203. Followup-To: comp.unix.questions
  204. Date: 24 Dec 1992 06:03:15 GMT
  205. Organization: Empress Software
  206. Lines: 555
  207. Approved: news-answers-request@MIT.Edu
  208. Distribution: world
  209. Expires: 21 Jan 1993 06:02:09 GMT
  210. Message-ID: <unix-faq/faq/part4_725176929@athena.mit.edu>
  211. References: <unix-faq/faq/contents_725176929@athena.mit.edu>
  212. NNTP-Posting-Host: pit-manager.mit.edu
  213. X-Last-Updated: 1992/12/09
  214.  
  215. Archive-name: unix-faq/faq/part4
  216. Version: $Id: part4,v 2.1 92/12/04 07:43:53 tmatimar Exp $
  217.  
  218. These seven articles contain the answers to some Frequently Asked
  219. Questions often seen in comp.unix.questions and comp.unix.shell.
  220. Please don't ask these questions again, they've been answered plenty
  221. of times already - and please don't flame someone just because they may
  222. not have read this particular posting.  Thank you.
  223.  
  224. These articles are divided approximately as follows:
  225.  
  226.       1.*) General questions.
  227.       2.*) Relatively basic questions, likely to be asked by beginners.
  228.       3.*) Intermediate questions.
  229.       4.*) Advanced questions, likely to be asked by people who thought
  230.        they already knew all of the answers.
  231.       5.*) Questions pertaining to the various shells, and the differences.
  232.       6.*) An overview of Unix variants.
  233.       7.*) An comparison of configuration management systems (RCS, SCCS).
  234.  
  235. This article includes answers to:
  236.  
  237.       4.1)  How do I read characters from a terminal without requiring the user
  238.               to hit RETURN?
  239.       4.2)  How do I check to see if there are characters to be read without
  240.               actually reading?
  241.       4.3)  How do I find the name of an open file?
  242.       4.4)  How can an executing program determine its own pathname?
  243.       4.5)  How do I use popen() to open a process for reading AND writing?
  244.       4.6)  How do I sleep() in a C program for less than one second?
  245.       4.7)  How can I get setuid shell scripts to work?
  246.       4.8)  How can I find out which user or process has a file open or is using
  247.             a particular file system (so that I can unmount it?)
  248.       4.9)  How do I keep track of people who are fingering me?
  249.       4.10) Is it possible to reconnect a process to a terminal after it has
  250.             been disconnected, e.g. after starting a program in the background
  251.             and logging out?
  252.       4.11) Is it possible to "spy" on a terminal, displaying the output
  253.             that's appearing on it on another terminal?
  254.  
  255. If you're looking for the answer to, say, question 4.5, and want to skip
  256. everything else, you can search ahead for the regular expression "^4.5)".
  257.  
  258. While these are all legitimate questions, they seem to crop up in
  259. comp.unix.questions or comp.unix.shell on an annual basis, usually
  260. followed by plenty of replies (only some of which are correct) and then
  261. a period of griping about how the same questions keep coming up.  You
  262. may also like to read the monthly article "Answers to Frequently Asked
  263. Questions" in the newsgroup "news.announce.newusers", which will tell
  264. you what "UNIX" stands for.
  265.  
  266. With the variety of Unix systems in the world, it's hard to guarantee
  267. that these answers will work everywhere.  Read your local manual pages
  268. before trying anything suggested here.  If you have suggestions or
  269. corrections for any of these answers, please send them to to
  270. tmatimar@empress.com.
  271.  
  272. 4.1)  How do I read characters from a terminal without requiring the user
  273.       to hit RETURN?
  274.  
  275.       Check out cbreak mode in BSD, ~ICANON mode in SysV.
  276.  
  277.       If you don't want to tackle setting the terminal parameters
  278.       yourself (using the "ioctl(2)" system call) you can let the stty
  279.       program do the work - but this is slow and inefficient, and you
  280.       should change the code to do it right some time:
  281.  
  282.       #include <stdio.h>
  283.       main()
  284.       {
  285.         int c;
  286.  
  287.         printf("Hit any character to continue\n");
  288.         /*
  289.          * ioctl() would be better here; only lazy
  290.          * programmers do it this way:
  291.          */
  292.         system("/bin/stty cbreak");        /* or "stty raw" */
  293.         c = getchar();
  294.         system("/bin/stty -cbreak");
  295.         printf("Thank you for typing %c.\n", c);
  296.  
  297.         exit(0);
  298.       }
  299.  
  300.       You might like to check out the documentation for the "curses"
  301.       library of portable screen functions.  Often if you're interested
  302.       in single-character I/O like this, you're also interested in
  303.       doing some sort of screen display control, and the curses library
  304.       provides various portable routines for both functions.
  305.  
  306. 4.2)  How do I check to see if there are characters to be read without
  307.       actually reading?
  308.  
  309.       Certain versions of UNIX provide ways to check whether characters
  310.       are currently available to be read from a file descriptor.  In
  311.       BSD, you can use select(2).  You can also use the FIONREAD ioctl
  312.       (see tty(4)), which returns the number of characters waiting to
  313.       be read, but only works on terminals, pipes and sockets.  In
  314.       System V Release 3, you can use poll(2), but that only works on
  315.       streams.  In Xenix - and therefore Unix SysV r3.2 and later - the
  316.       rdchk() system call reports whether a read() call on a given file
  317.       descriptor will block.
  318.  
  319.       There is no way to check whether characters are available to be
  320.       read from a FILE pointer.  (You could poke around inside stdio
  321.       data structures to see if the input buffer is nonempty, but that
  322.       wouldn't work since you'd have no way of knowing what will happen
  323.       the next time you try to fill the buffer.)
  324.  
  325.       Sometimes people ask this question with the intention of writing
  326.         if (characters available from fd)
  327.             read(fd, buf, sizeof buf);
  328.       in order to get the effect of a nonblocking read.  This is not
  329.       the best way to do this, because it is possible that characters
  330.       will be available when you test for availability, but will no
  331.       longer be available when you call read.  Instead, set the
  332.       O_NDELAY flag (which is also called FNDELAY under BSD) using the
  333.       F_SETFL option of fcntl(2).  Older systems (Version 7, 4.1 BSD)
  334.       don't have O_NDELAY; on these systems the closest you can get to
  335.       a nonblocking read is to use alarm(2) to time out the read.
  336.  
  337. 4.3)  How do I find the name of an open file?
  338.  
  339.       In general, this is too difficult.  The file descriptor may
  340.       be attached to a pipe or pty, in which case it has no name.
  341.       It may be attached to a file that has been removed.  It may
  342.       have multiple names, due to either hard or symbolic links.
  343.  
  344.       If you really need to do this, and be sure you think long
  345.       and hard about it and have decided that you have no choice,
  346.       you can use find with the -inum and possibly -xdev option,
  347.       or you can use ncheck, or you can recreate the functionality
  348.       of one of these within your program.  Just realize that
  349.       searching a 600 megabyte filesystem for a file that may not
  350.       even exist is going to take some time.
  351.  
  352. 4.4)  How can an executing program determine its own pathname?
  353.  
  354.       Your program can look at argv[0]; if it begins with a "/", it is
  355.       probably the absolute pathname to your program, otherwise your
  356.       program can look at every directory named in the environment
  357.       variable PATH and try to find the first one that contains an
  358.       executable file whose name matches your program's argv[0] (which
  359.       by convention is the name of the file being executed).  By
  360.       concatenating that directory and the value of argv[0] you'd
  361.       probably have the right name.
  362.  
  363.       You can't really be sure though, since it is quite legal for one
  364.       program to exec() another with any value of argv[0] it desires.
  365.       It is merely a convention that new programs are exec'd with the
  366.       executable file name in argv[0].
  367.  
  368.       For instance, purely a hypothetical example:
  369.     
  370.     #include <stdio.h>
  371.     main()
  372.     {
  373.         execl("/usr/games/rogue", "vi Thesis", (char *)NULL);
  374.     }
  375.  
  376.       The executed program thinks its name (its argv[0] value) is
  377.       "vi Thesis".   (Certain other programs might also think that
  378.       the name of the program you're currently running is "vi Thesis",
  379.       but of course this is just a hypothetical example, don't
  380.       try it yourself :-)
  381.  
  382. 4.5)  How do I use popen() to open a process for reading AND writing?
  383.  
  384.       The problem with trying to pipe both input and output to an
  385.       arbitrary slave process is that deadlock can occur, if both
  386.       processes are waiting for not-yet-generated input at the same
  387.       time.  Deadlock can be avoided only by having BOTH sides follow a
  388.       strict deadlock-free protocol, but since that requires
  389.       cooperation from the processes it is inappropriate for a
  390.       popen()-like library function.
  391.  
  392.       The 'expect' distribution includes a library of functions that a
  393.       C programmer can call directly.  One of the functions does the
  394.       equivalent of a popen for both reading and writing.  It uses ptys
  395.       rather than pipes, and has no deadlock problem.  It's portable to
  396.       both BSD and SV.  See the next answer for more about 'expect'.
  397.  
  398. 4.6)  How do I sleep() in a C program for less than one second?
  399.  
  400.       The first thing you need to be aware of is that all you can
  401.       specify is a MINIMUM amount of delay; the actual delay will
  402.       depend on scheduling issues such as system load, and could be
  403.       arbitrarily large if you're unlucky.
  404.  
  405.       There is no standard library function that you can count on in
  406.       all environments for "napping" (the usual name for short
  407.       sleeps).  Some environments supply a "usleep(n)" function which
  408.       suspends execution for n microseconds.  If your environment
  409.       doesn't support usleep(), here are a couple of implementations
  410.       for BSD and System V environments.
  411.  
  412.       The following code is adapted from Doug Gwyn's System V emulation
  413.       support for 4BSD and exploits the 4BSD select() system call.
  414.       Doug originally called it 'nap()'; you probably want to call it
  415.       "usleep()";
  416.  
  417.       /*
  418.         usleep -- support routine for 4.2BSD system call emulations
  419.         last edit:    29-Oct-1984    D A Gwyn
  420.       */
  421.  
  422.       extern int    select();
  423.  
  424.       int
  425.       usleep( usec )                /* returns 0 if ok, else -1 */
  426.         long        usec;        /* delay in microseconds */
  427.         {
  428.         static struct            /* `timeval' */
  429.             {
  430.             long    tv_sec;        /* seconds */
  431.             long    tv_usec;    /* microsecs */
  432.             }    delay;        /* _select() timeout */
  433.  
  434.         delay.tv_sec = usec / 1000000L;
  435.         delay.tv_usec = usec % 1000000L;
  436.  
  437.         return select( 0, (long *)0, (long *)0, (long *)0, &delay );
  438.         }
  439.  
  440.       On System V you might do it this way:
  441.  
  442.       /*
  443.       subseconds sleeps for System V - or anything that has poll()
  444.       Don Libes, 4/1/1991
  445.  
  446.       The BSD analog to this function is defined in terms of
  447.       microseconds while poll() is defined in terms of milliseconds.
  448.       For compatibility, this function provides accuracy "over the long
  449.       run" by truncating actual requests to milliseconds and
  450.       accumulating microseconds across calls with the idea that you are
  451.       probably calling it in a tight loop, and that over the long run,
  452.       the error will even out.
  453.  
  454.       If you aren't calling it in a tight loop, then you almost
  455.       certainly aren't making microsecond-resolution requests anyway,
  456.       in which case you don't care about microseconds.  And if you did,
  457.       you wouldn't be using UNIX anyway because random system
  458.       indigestion (i.e., scheduling) can make mincemeat out of any
  459.       timing code.
  460.  
  461.       Returns 0 if successful timeout, -1 if unsuccessful.
  462.  
  463.       */
  464.  
  465.       #include <poll.h>
  466.  
  467.       int
  468.       usleep(usec)
  469.       unsigned int usec;        /* microseconds */
  470.       {
  471.         static subtotal = 0;    /* microseconds */
  472.         int msec;            /* milliseconds */
  473.  
  474.         /* 'foo' is only here because some versions of 5.3 have
  475.          * a bug where the first argument to poll() is checked
  476.          * for a valid memory address even if the second argument is 0.
  477.          */
  478.         struct pollfd foo;
  479.  
  480.         subtotal += usec;
  481.         /* if less then 1 msec request, do nothing but remember it */
  482.         if (subtotal < 1000) return(0);
  483.         msec = subtotal/1000;
  484.         subtotal = subtotal%1000;
  485.         return poll(&foo,(unsigned long)0,msec);
  486.       }
  487.  
  488.       Another possibility for nap()ing on System V, and probably other
  489.       non-BSD Unices is Jon Zeeff's s5nap package, posted to
  490.       comp.sources.misc, volume 4.  It does require a installing a
  491.       device driver, but works flawlessly once installed.  (Its
  492.       resolution is limited to the kernel HZ value, since it uses the
  493.       kernel delay() routine.)
  494.  
  495. 4.7)  How can I get setuid shell scripts to work?
  496.  
  497.       [ This is a long answer, but it's a complicated and frequently-asked
  498.         question.  Thanks to Maarten Litmaath for this answer, and
  499.         for the "indir" program mentioned below. ]
  500.  
  501.       Let us first assume you are on a UNIX variant (e.g. 4.3BSD or
  502.       SunOS) that knows about so-called `executable shell scripts'.
  503.       Such a script must start with a line like:
  504.  
  505.     #!/bin/sh
  506.  
  507.       The script is called `executable' because just like a real (binary)
  508.       executable it starts with a so-called `magic number' indicating
  509.       the type of the executable.  In our case this number is `#!' and
  510.       the OS takes the rest of the first line as the interpreter for
  511.       the script, possibly followed by 1 initial option like:
  512.  
  513.     #!/bin/sed -f
  514.  
  515.       Suppose this script is called `foo' and is found in /bin,
  516.       then if you type:
  517.  
  518.     foo arg1 arg2 arg3
  519.  
  520.       the OS will rearrange things as though you had typed:
  521.  
  522.     /bin/sed -f /bin/foo arg1 arg2 arg3
  523.  
  524.       There is one difference though: if the setuid permission bit for
  525.       `foo' is set, it will be honored in the first form of the
  526.       command; if you really type the second form, the OS will honor
  527.       the permission bits of /bin/sed, which is not setuid, of course.
  528.  
  529.       ----------
  530.  
  531.       OK, but what if my shell script does NOT start with such a `#!'
  532.       line or my OS does not know about it?
  533.  
  534.       Well, if the shell (or anybody else) tries to execute it, the OS
  535.       will return an error indication, as the file does not start with
  536.       a valid magic number.  Upon receiving this indication the shell
  537.       ASSUMES the file to be a shell script and gives it another try:
  538.  
  539.     /bin/sh shell_script arguments
  540.  
  541.       But we have already seen that a setuid bit on `shell_script' will
  542.       NOT be honored in this case!
  543.  
  544.       ----------
  545.  
  546.       Right, but what about the security risks of setuid shell scripts?
  547.  
  548.       Well, suppose the script is called `/etc/setuid_script', starting
  549.       with:
  550.  
  551.     #!/bin/sh
  552.     
  553.       Now let us see what happens if we issue the following commands:
  554.  
  555.     $ cd /tmp
  556.     $ ln /etc/setuid_script -i
  557.     $ PATH=.
  558.     $ -i
  559.  
  560.       We know the last command will be rearranged to:
  561.  
  562.     /bin/sh -i
  563.  
  564.       But this command will give us an interactive shell, setuid to the
  565.       owner of the script!
  566.       Fortunately this security hole can easily be closed by making the
  567.       first line:
  568.  
  569.     #!/bin/sh -
  570.  
  571.       The `-' signals the end of the option list: the next argument `-i'
  572.       will be taken as the name of the file to read commands from, just
  573.       like it should!
  574.  
  575.       ---------
  576.  
  577.       There are more serious problems though:
  578.  
  579.     $ cd /tmp
  580.     $ ln /etc/setuid_script temp
  581.     $ nice -20 temp &
  582.     $ mv my_script temp
  583.  
  584.       The third command will be rearranged to:
  585.  
  586.     nice -20 /bin/sh - temp
  587.  
  588.       As this command runs so slowly, the fourth command might be able
  589.       to replace the original `temp' with `my_script' BEFORE `temp' is
  590.       opened by the shell!  There are 4 ways to fix this security hole:
  591.  
  592.     1)  let the OS start setuid scripts in a different, secure way
  593.         - System V R4 and 4.4BSD use the /dev/fd driver to pass the
  594.         interpreter a file descriptor for the script
  595.  
  596.     2)  let the script be interpreted indirectly, through a frontend
  597.         that makes sure everything is all right before starting the
  598.         real interpreter - if you use the `indir' program from
  599.         comp.sources.unix the setuid script will look like this:
  600.  
  601.         #!/bin/indir -u
  602.         #?/bin/sh /etc/setuid_script
  603.  
  604.     3)  make a `binary wrapper': a real executable that is setuid and
  605.         whose only task is to execute the interpreter with the name of
  606.         the script as an argument
  607.  
  608.     4)  make a general `setuid script server' that tries to locate the
  609.         requested `service' in a database of valid scripts and upon
  610.         success will start the right interpreter with the right
  611.         arguments.
  612.  
  613.       ---------
  614.  
  615.       Now that we have made sure the right file gets interpreted, are
  616.       there any risks left?
  617.  
  618.       Certainly!  For shell scripts you must not forget to set the PATH
  619.       variable to a safe path explicitly.  Can you figure out why?
  620.       Also there is the IFS variable that might cause trouble if not
  621.       set properly.  Other environment variables might turn out to
  622.       compromise security as well, e.g. SHELL...  Furthermore you must
  623.       make sure the commands in the script do not allow interactive
  624.       shell escapes!  Then there is the umask which may have been set
  625.       to something strange...
  626.  
  627.       Etcetera.  You should realise that a setuid script `inherits' all
  628.       the bugs and security risks of the commands that it calls!
  629.  
  630.       All in all we get the impression setuid shell scripts are quite a
  631.       risky business!  You may be better off writing a C program instead!
  632.  
  633. 4.8)  How can I find out which user or process has a file open or is using
  634.       a particular file system (so that I can unmount it?)
  635.  
  636.       Use fuser (system V), fstat (BSD), ofiles (public domain) or
  637.       pff (public domain).  These programs will tell you various things
  638.       about processes using particular files.
  639.  
  640.       A port of the 4.3 BSD fstat to Dynix, SunOS and Ultrix
  641.       can be found in archives of comp.sources.unix, volume 18.
  642.  
  643.       pff is part of the kstuff package, and works on quite a few systems.
  644.       Instructions for obtaining kstuff are provided in question 3.10.
  645.  
  646. 4.9)  How do I keep track of people who are fingering me?
  647.  
  648.       From: jik@pit-manager.MIT.EDU (Jonathan I. Kamens)
  649.       From: malenovi@plains.NoDak.edu (Nikola Malenovic)
  650.       Date: Mon, 23 Nov 1992 16:01:45 -0600
  651.  
  652.       Generally, you can't find out the userid of someone who is
  653.       fingering you from a remote machine.  You may be able to
  654.       find out which machine the remote request is coming from.
  655.       One possibility, if your system supports it and assuming
  656.       the finger daemon doesn't object, is to make your .plan file a
  657.       "named pipe" instead of a plain file.  (Use 'mknod' to do this.)
  658.